package priv.pront.code.lanqiao.competition.course.date0331;

import java.util.HashMap;
import java.util.Scanner;

/**
 * @Description:
 * @Author: pront
 * @Time:2023-03-31 16:22
 */
public class 交换次数 {

        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            String string = scanner.next();
            HashMap<Character,Integer> stringmap = new HashMap();
        /*
         * 测试用例长度
         * while (string.length() < 100000) {
            string += string;
            }
        */
            // 统计ABT三个字母的次数
            for (int i = 0; i < string.length(); i++) {
                char charAt = string.charAt(i);
                if (stringmap.containsKey(charAt)) {
                    stringmap.put(charAt, stringmap.get(charAt) + 1);
                } else {
                    stringmap.put(charAt, 1);
                }
            }
            char[] carr = {'A', 'B', 'T'};
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    for (int j2 = 0; j2 < 3; j2++) {
                        if (i != j && i != j2 && j != j2) {
                            // ABT全排列有6种方式
                            // s1表示第一个字母长度的字符串，s2表示第二个字母长度的字符串,s3表示第三个字母长度的字符串
                            // c1、c2、c3表示字母的排列方式，如排列方式为BAT的话，则c1 = 'B', c2 = 'A', c3 = 'T'
                            String s1 = string.substring(0, stringmap.get(carr[i]));
                            String s2 = string.substring(s1.length(), s1.length() + stringmap.get(carr[j]));
                            cal(s1, s2, carr[i], carr[j], carr[j2]);
                        }
                    }
                }
            }
            System.out.println(result);
        }

        static int result = Integer.MAX_VALUE;

        private static void cal(String s1, String s2, char c1, char c2, char c3) {
            // 统计s1中字母的个数
            HashMap<Character,Integer> map1 = new HashMap();
            map1.put(c1, 0);
            map1.put(c2, 0);
            map1.put(c3, 0);
            for (int i = 0; i < s1.length(); i++) {
                char charAt = s1.charAt(i);
                map1.put(charAt, map1.get(charAt) + 1);
            }
            // 统计s2中字母的个数
            HashMap<Character, Integer> map2 = new HashMap();
            map2.put(c1, 0);
            map2.put(c2, 0);
            map2.put(c3, 0);
            for (int i = 0; i < s2.length(); i++) {
                char charAt = s2.charAt(i);
                map2.put(charAt, map2.get(charAt) + 1);
            }
            // 总需交换次数为s1.length()-s1(c1)+s2.length()-s2(c2)-Math.min(s2(c1),s1(c2))
            // 第一字母交换次数+第二字母交换次数-第二字母刚好在第一字母长度内的个数与第一字母刚好在第二字母长度内的个数的最小值
            int sum = s1.length() - map1.get(c1) + s2.length() - map2.get(c2) - Math.min(map1.get(c2), map2.get(c1));
            result = Math.min(sum, result);
        }
}
